package com.leenleda.ips.util;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.iflytek.speech.ISpeechModule;
import com.iflytek.speech.InitListener;
import com.iflytek.speech.SpeechConstant;
import com.iflytek.speech.SpeechSynthesizer;
import com.iflytek.speech.SynthesizerListener;
import com.leenleda.ips.BaseActivity;
import com.leenleda.ips.receiver.Constant;

public class TTSUtils implements InitListener {


    private static volatile TTSUtils instance = null;

    private Context context = null;
    private static String tag = "TTSUtils";

    public static TTSUtils getInstance() {
        if (instance == null) {
            synchronized (TTSUtils.class) {
                if (instance == null) {
                    instance = new TTSUtils();
//                    instance.init();
                }
            }
        }
        return instance;
    }

    public void initTTs(Context context){
        if (!initSuccess){
            this.context = context;
            mTts = new SpeechSynthesizer(context, this);
            Log.e(tag, "-----init------" + mTts);
            return;
        }
    }

    /**
     * 设置语速
     */
    public void speak(Context context, String msg, int num, String speed) {
        if (mTts != null)
            mTts.setParameter(SpeechSynthesizer.SPEED, speed);
        speak(context, msg, num);
    }

    /**
     * 设置数字读取方式
     * 2 123
     * 1 一百二十三
     */
    public void speak(Context context, String msg, String rdn, int num) {
        if (mTts != null)
            mTts.setParameter("rdn", rdn);
        speak(context, msg, num);
    }


    /**
     * 设置数字读取方式
     * 设置语速
     */
    public void speak(Context context, String msg, int num, String speed, String rdn) {
        if (mTts != null) {
            mTts.setParameter("rdn", rdn);
            mTts.setParameter(SpeechSynthesizer.SPEED, speed);
        }
        speak(context, msg, num);
    }

//        mTts.setParameter("rdn", "2");
    private SpeechSynthesizer mTts;

    private static int speakNum = 0;
    private static String strData = "";
    public static boolean isSpeaking = false;
    public void speak(Context context, String msg, int num) {
        Log.e(tag, "-----speakNum---num==" + num + ",voiceSpeed=" + Constant.voiceSpeed);
        strData = msg;
        speakNum = num;
        isSpeaking = true;
        if (!initSuccess){
            this.context = context;
            mTts = new SpeechSynthesizer(context, this);
            Log.e(tag, "-----init------" + mTts);
            return;
        }
        Log.e(tag, "-----TTS----voiceRdn-1***********-" + Constant.voiceRdn + ",speed=" + Constant.voiceSpeed);
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechSynthesizer.TTS_ENGINE_TYPE_LOCAL);
//        mTts.setParameter(SpeechSynthesizer.VOICE_NAME, "xiaoyan");
        mTts.setParameter(SpeechSynthesizer.SPEED, Constant.voiceSpeed);
        mTts.setParameter(SpeechSynthesizer.PITCH, "40");
        mTts.setParameter(SpeechSynthesizer.VOLUME, "100");
        if (Constant.voiceRdn.equals("2")){
            msg = getDataS(msg);
        }

//        mTts.setParameter(SpeechSynthesizer.SPEED, "45");
//        mTts.setParameter(SpeechSynthesizer.PITCH, "55");
//        mTts.setParameter(SpeechConstant.PARAMS, "tts_audio_path=/sdcard/speak_result.pcm");

        int code = mTts.startSpeaking(msg, mTtsListener);
        Log.e(tag, "-----code------" + code + "==msg==" + msg);
    }

    private String getDataS(String msg){
        return msg.replace("1", "一")
                .replace("2", "二")
                .replace("3", "三")
                .replace("4", "四")
                .replace("5", "五")
                .replace("6", "六")
                .replace("7", "七")
                .replace("8", "八")
                .replace("9", "九")
                .replace("0", "零");
    }


    public void speak(Context context, String msg, String speed, String pitch) {
        Log.e(tag, "-----speakNum---speed==" + speed + ",pitch=" +pitch);
        strData = msg;
        speakNum = 1;
        isSpeaking = true;
        if (!initSuccess){
            this.context = context;
            mTts = new SpeechSynthesizer(context, this);
            Log.e(tag, "-----init------" + mTts);
            return;
        }
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechSynthesizer.TTS_ENGINE_TYPE_LOCAL);
//        mTts.setParameter(SpeechSynthesizer.VOICE_NAME, "xiaoyan");
        mTts.setParameter(SpeechSynthesizer.SPEED, speed);
        mTts.setParameter(SpeechSynthesizer.PITCH, pitch);
        mTts.setParameter(SpeechSynthesizer.VOLUME, "100");
        if (!TextUtils.isEmpty(Constant.voiceRdn))//数字发声方式 eg(123): 1,一百二十三  2,一二三
            mTts.setParameter("rdn", Constant.voiceRdn);
//        mTts.setParameter(SpeechSynthesizer.SPEED, "45");
//        mTts.setParameter(SpeechSynthesizer.PITCH, "55");
        mTts.setParameter(SpeechConstant.PARAMS, "tts_audio_path=/sdcard/speak_result.pcm");
//        mTts.setParameter(SpeechConstant.PARAMS, "tts_audio_path=/sdcard/speak_result.mp3");

        int code = mTts.startSpeaking(msg, mTtsListener);
        Log.e(tag, "-----code------" + code + "==msg==" + msg);
    }


    private SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {
        public void onBufferProgress(int progress) {
        }

        public void onCompleted(int code) {
            Log.e(tag, "-----onCompleted------" + code + "-----speakNum---1==" + speakNum);
            speakNum--;
            if (speakNum > 0){
                speak(context, strData, speakNum);
                return;
            }
            strData = "";
            isSpeaking = false;

            Log.e(tag, "-----speakNum---2==" + speakNum);
            if (speakNum < 0){//表示不需要回掉
                speakNum = 0;
                return;
            }
            Log.e(tag, "-----speakNum---3==" + speakNum);
            speakNum = 0;
            if (code != 0)
                return;
            String js = String.format("javascript:getVoiceRes();");
            Intent intent = new Intent(Constant.TIMER_LOOP_ACTION);
            Bundle bundle = new Bundle();
            bundle.putInt("state", Constant.REFRESH_JS);
            bundle.putString("data", js);
            intent.putExtras(bundle);
            BaseActivity.getForegroundActivity().sendBroadcast(intent);
        }

        public void onSpeakBegin() throws RemoteException {
//            ePlayerImpl.pauseVideoVolume();
        }

        public void onSpeakPaused() throws RemoteException {
        }

        public void onSpeakProgress(int progress) throws RemoteException {
        }

        public void onSpeakResumed() throws RemoteException {
        }
    };

    private boolean initSuccess = false;
    @Override
    public void onInit(ISpeechModule iSpeechModule, int code) {

        Log.e(tag, "-----onInit  ------" + code);
        if (code == 0) {
            initSuccess = true;
            speak(context, strData, speakNum);
        }
    }
}
